#pragma once
#include <iostream>
#include "1my_protocol.hpp"

enum
{
    Div_Zero = 1,
    Mod_Zero,
    Op_Err
};

class Server
{
public:
    Response Calculator(const Request& req)
    {
        Response res(0, 0);
        switch(req._op)
        {
        case '+':
            res._result = req._x + req._y;
            break;
        case '-':
            res._result = req._x - req._y;
            break;
        case '*':
            res._result = req._x * req._y;
            break;
        case '/':
            {
                if(req._y == 0)
                {
                    res._code = Div_Zero;
                }
                else
                {
                    res._result = req._x / req._y;
                }
            }
            break;
        case '%':
            {
                if(req._y == 0)
                {
                    res._code = Mod_Zero;
                }
                else
                {
                    res._result = req._x % req._y;
                }
            }
            break;
        default:
            res._code = Op_Err;
            break;
        }

        return res;
    }

    std::string HandlePackage(std::string& package_stream)
    {
        std::string content;
        bool d = Decode(package_stream, &content);
        if(d == false)
        {
            return "";
        }

        Request req;
        req.Deserialize(content);

        Response res = Calculator(req);

        std::string res_content;
        res.Serialize(&res_content);
        std::string package = Encode(res_content);

        return package;
    }
};
